package course.dmsSpringBoot.controller;

import course.dmsSpringBoot.entity.Customer;
import course.dmsSpringBoot.entity.ResultMap;
import course.dmsSpringBoot.service.CustomerService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户管理
 * @author zouran
 * createDate：2023/12/17  23:09
 */
@RestController
@RequestMapping("/customer")
public class CustomerController  {
    private final CustomerService customerService;
    private final ResultMap<Object> resultMap;

    public CustomerController(CustomerService customerService, ResultMap<Object> resultMap) {
        this.customerService = customerService;
        this.resultMap = resultMap;
    }
    @PostMapping("/updateCustomer")
    public ResultMap<Object> updateCustomer(@RequestBody Customer customer) {
        if (customerService.updateById(customer))
         return resultMap.successWithMessage("客户信息更新成功！");
        else return resultMap.codeAndMessage(201,"客户信息更新失败！Id不存在");
    }
    @RequestMapping("/queryCustomerByName")
    public ResultMap<Object> queryCustomerByName(String name) {
        return resultMap.success(customerService.queryCustomerByName(name));
    }

    @RequestMapping("/queryAllCustomer")
    public ResultMap<List<Customer>> queryAllCustomer() {
        return resultMap.success(customerService.list());
    }
    @RequestMapping("/deleteCustomerById")
    public ResultMap<Object> deleteCustomerById(int id) {
        if (customerService.removeById(id)){
            return resultMap.successWithMessage("删除成功!");
        }
        return resultMap.successWithMessage("客户id不存在!");
    }
}
